import { PatientStateITF } from '@REDUXITF/patient'
import { PatientITF } from '@INTERFACES/Patient'
import { CREATE_CLICK_BTN_TYPE } from '@CONSTANTS/painLog'
import { showModal } from '@UTILS/common'
import { EVENT_DIALOG_DATA } from '@CONSTANTS/app'

export const convertCreateLoggPayload = (data: {
  props?: PatientStateITF, isPlanTask?: boolean,
  painLogPatient?: PatientITF, type: CREATE_CLICK_BTN_TYPE
}) => {
  const { props, painLogPatient: logPatient, type } = data
  const {
    painLocations, painHeadSites, painOverallLevel, painActivityLevel, painConcomitantSymptom,
    painTriggers, painRelief, painTreatments,
    painMedications, painMood, painSleep, painOther, reliefText, painSymptoms, painAuras,
    triggerText, treatmentText, moodText, concomitantSymptomText, painFrequency, painTotalTimeMinute,
    symptomText, auraText, painLogPatient, painLogId = '', dateBucket, painSessions
  } = props || {}

  const { p8Id, PatientId } = logPatient || painLogPatient || {}
  if (type === CREATE_CLICK_BTN_TYPE.SAVE_NO_PAIN_LOG) {
    return {
      PatientId: p8Id || PatientId,
      OverallRating: 0,
      PainLogId: painLogId
    }
  }
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  const Medications = painMedications.map(({ id, ...Name }) => ({
    MedicineId: id,
    Name
  }))
  const locations = painLocations?.map(BodyMap => ({
    BodyMap,
    Rating: 0,
    Symptoms: []
  }))
  const headSites = painHeadSites?.map(HeadMap => ({
    HeadMap,
    Rating: 0,
    Symptoms: []
  }))
  const payload = {
    PatientId: p8Id || PatientId,
    OverallRating: painOverallLevel,
    PainSites: locations,
    HeadPainSites: headSites,
    ActivityLevel: painActivityLevel,
    Relief: painRelief,
    ConcomitantSymptom: painConcomitantSymptom,
    Triggers: painTriggers,
    Treatments: painTreatments,
    Symptoms: painSymptoms,
    Auras: painAuras,
    Medications,
    Mood: painMood,
    Sleep: painSleep,
    Notes: painOther,
    Frequency: painFrequency,
    TotalTimeMinute: painTotalTimeMinute,
    ConcomitantSymptomText: concomitantSymptomText || '',
    TriggerText: triggerText || '',
    ReliefText: reliefText || '',
    TreatmentText: treatmentText || '',
    MoodText: moodText || '',
    SymptomText: symptomText || '',
    AuraText: auraText || '',
    PainLogId: painLogId,
    DateBucket: dateBucket,
    Sessions: painSessions
  }
  return payload
}

export const showSaveLogModal = (callBack: (change?: boolean)=> void, hasOther: boolean, page: string) => {
  if (!hasOther) {
    callBack?.()
    return
  }
  const { Type } = EVENT_DIALOG_DATA
  showModal({
    eventData: { Type: Type.ChangeSite, Page: page },
    content: '更改部位将丢失您已编辑的其他内容，确定更改？',
    confirm: () => {
      callBack?.(true)
    },
  })
}
